package com.hsj.goldstore.mvp.model;

import android.util.Log;

import com.google.gson.Gson;
import com.hsj.goldstore.base.BaseBean;
import com.hsj.goldstore.base.BaseModel;
import com.hsj.goldstore.base.ResultCallback;
import com.hsj.goldstore.bean.LoginBean;
import com.hsj.goldstore.utils.LogUtil;
import com.hsj.goldstore.utils.SignUtil;
import com.hsj.goldstore.utils.rxjava.ApiCaback;
import com.hsj.goldstore.utils.rxjava.ObserCallback;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/3/22.
 */

public class LoginModel extends BaseModel implements ILoginModel {
    private static final String TAG = "LoginModel";

    @Override
    public void chekToken(final ResultCallback callback) {

        addSubscribe(apiStore.tokenCheck(), new ObserCallback(new ApiCaback() {
            @Override
            public void onSuccess(Object data) {
                callback.onSuccess(data, 0, "请求成功");
            }

            @Override
            public void onFailure(int code, String msg) {
                LogUtil.d("info", code + msg);
                callback.onFalure(code, msg);
            }
        }));
    }

    /**
     * @param phone
     * @param pwd
     * @param callback 登录接口
     */
    @Override
    public void login(String phone, String pwd, final ResultCallback callback) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", phone);
        map.put("password", pwd);
        sign(map);
        LogUtil.d("info", new Gson().toJson(map));

        addSubscribe(apiStore.login(map), new ObserCallback(new ApiCaback<BaseBean<LoginBean>>() {
            @Override
            public void onSuccess(BaseBean<LoginBean> data) {
                    if (data.getCode() == 1001) {
                        callback.onFalure( data.getCode(), (String) data.getMsg());
                    } else {
                        callback.onSuccess(data.getData(), 0, "登录成功");
                    }
            }

            @Override
            public void onFailure(int code, String msg) {
                callback.onFalure(code, msg);
         }
        }));
    }
}
